/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package xin.petstore.framework.utils;

import java.security.InvalidParameterException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.lang3.*;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * @author ThinkGem
 * @version 2014-4-15
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	
	private static String[] parsePatterns = {
		"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
		"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
		"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}
	
	/**
	 * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}

	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}

	/**
	 * 解析时间("yyyy-MM-dd hh:mm:ss")
	 * @param dateString
	 * @return Date
	 * @author mxm
	 */
	public static final Date parseTime(String dateString){
		return parse(dateString, "yyyy-MM-dd HH:mm:ss");
	}
	public static final int YEAR = 1;
	public static final int MONTH = 2;
	public static final int DAY = 5;
	public static final int HOUR = 10;
	public static final int HOUR_OF_DAY = 11;
	public static final int MINUTE = 12;
	public static final int SECOND = 13;
	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String TIME_FORMAT = "HH:mm:ss";

	/**
	 * 日期型字符串转化为日期 格式
	 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", 
	 *   "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
	 *   "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
	 */
	public static Date parseDate(Object str) {
		if (str == null){
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000);
	}

	/**
	 * 获取过去的小时
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*60*1000);
	}
	
	/**
	 * 获取过去的分钟
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*1000);
	}
	
	/**
	 * 转换为时间（天,时:分:秒.毫秒）
	 * @param timeMillis
	 * @return
	 */
    public static String formatDateTime(long timeMillis){
		long day = timeMillis/(24*60*60*1000);
		long hour = (timeMillis/(60*60*1000)-day*24);
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
    }
	
	/**
	 * 获取两个日期之间的天数
	 * 
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
	}

	/**
	 * 获得当前系统时间
	 * 格式yyyy-MM-dd hh:mm:ss
	 * @return Date
	 * @author mxm
	 */
	public static final Date getCurrentTime(){
		return Calendar.getInstance().getTime();
	}

	/**
	 * 获得当前系统时间
	 * 格式 yyyy-MM-dd
	 * @return Date
	 * @author mxm
	 */
	public static final Date getCurrentDate(){
		return getDateIgnoreTime(Calendar.getInstance().getTime());
	}

	/**
	 * 获取时间上下间隔
	 * @param baseDate
	 * @return Date[]
	 * @author mxm
	 */
	public static final Date[] getDifferenceDate(Date baseDate) {
		Date[] date = new Date[2];
		Calendar cal = Calendar.getInstance();
		cal.setTime(baseDate);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		date[0] = cal.getTime();
		cal.add(Calendar.DATE, 1);
		date[1] = cal.getTime();
		return date;
	}

	/**
	 * 将时间的时分秒毫秒归零
	 * @param baseDate
	 * @return Date
	 * @author mxm
	 */
	public static final Date getDateIgnoreTime(Date baseDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(baseDate);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 比较两个日期差,精确到天
	 * @param date1
	 * @param date2
	 * @return int
	 * @author mxm
	 */
	public static final int compareByDate(Date date1, Date date2) {
		int num = getDaysBetweenDates(date1, date2);
		if (num > 0)
			return 1;
		else if (num < 0)
			return -1;
		return num;
	}

	/**
	 * 获得两个日期差几天,精确到秒
	 * @param date1
	 * @param date2
	 * @return int
	 * @author mxm
	 */
	public static final int compareByDateTime(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			throw new IllegalArgumentException("Date cannot be null.");
		}
		TimeZone timeZone = TimeZone.getDefault();
		long beginOffset = timeZone.getRawOffset();
		long endOffset = beginOffset;
		if (timeZone.inDaylightTime(date1)) beginOffset += timeZone.getDSTSavings();
		if (timeZone.inDaylightTime(date2)) endOffset += timeZone.getDSTSavings();
		long milli1 = (date1.getTime() + beginOffset) / 1000;
		long milli2 = (date2.getTime() + endOffset) / 1000;
		int retVal = 0;
		if (milli1 > milli2) {
			retVal = 1;
		} else if (milli1 < milli2) {
			retVal = -1;
		}
		return retVal;
	}

	/**
	 * 获得两个日期差几天,精确到毫秒
	 * @param date1
	 * @param date2
	 * @return int
	 * @author mxm
	 */
	public static final int compareByTimestamp(Date date1, Date date2) {
		if(date1 ==null || date2 == null) {
			throw new IllegalArgumentException("Date cannot be null.");
		}
		TimeZone timeZone = TimeZone.getDefault();
		long beginOffset = timeZone.getRawOffset();
		long endOffset = beginOffset;
		if (timeZone.inDaylightTime(date1)) beginOffset += timeZone.getDSTSavings();
		if (timeZone.inDaylightTime(date2)) endOffset += timeZone.getDSTSavings();
		long milli1=date1.getTime() + beginOffset;
		long milli2=date2.getTime() + endOffset;
		int retVal = 0;
		if (milli1 > milli2) {
			retVal = 1;
		} else if (milli1 < milli2) {
			retVal = -1;
		}
		return retVal;
	}

	/**
	 * 获得两个日期差几天
	 * @param endDate
	 * @param beginDate
	 * @return int
	 * @author mxm
	 */
	public static final int getDaysBetweenDates(Date endDate, Date beginDate) {
		if (endDate == null || beginDate == null) {
			throw new IllegalArgumentException("Date cannot be null.");
		}
		return(getDaysBetweenDates((TimeZone)null, endDate, beginDate));
	}

	/**
	 * 获得两个日期差几天
	 * @param timeZone
	 * @param endDate
	 * @param beginDate
	 * @return int
	 * @author mxm
	 */
	public static final int getDaysBetweenDates(TimeZone timeZone, Date endDate, Date beginDate) {
		if (beginDate == null || endDate == null)
			throw new IllegalArgumentException("Date cannot be null.");
		if (timeZone == null) timeZone = TimeZone.getDefault();
		long beginOffset = timeZone.getRawOffset();
		long endOffset = beginOffset;
		if (timeZone.inDaylightTime(beginDate)) beginOffset += timeZone.getDSTSavings();
		if (timeZone.inDaylightTime(endDate)) endOffset += timeZone.getDSTSavings();
		long endDays = (long)((endDate.getTime() + endOffset) / 86400000L);
		long beginDays = (long)((beginDate.getTime() + beginOffset) / 86400000L);
		return((int)(endDays - beginDays));
	}

	/**
	 * 获得两个日期时间差,精确到毫秒
	 * @param endDate
	 * @param beginDate
	 * @return
	 * @author mxm
	 */
	public static final long getTimesBetweenDates(Date endDate, Date beginDate) {
		if (endDate == null || beginDate == null) {
			throw new IllegalArgumentException("Date cannot be null.");
		}
		return(getTimesBetweenDates((TimeZone)null, endDate, beginDate));
	}

	/**
	 * 获得两个日期时间差,精确到毫秒
	 * @param timeZone
	 * @param endDate
	 * @param beginDate
	 * @return
	 * @author mxm
	 */
	public static final long getTimesBetweenDates(TimeZone timeZone, Date endDate, Date beginDate) {
		if (beginDate == null || endDate == null)
			throw new IllegalArgumentException("Date cannot be null.");
		if (timeZone == null) timeZone = TimeZone.getDefault();
		long beginOffset = timeZone.getRawOffset();
		long endOffset = beginOffset;
		if (timeZone.inDaylightTime(beginDate)) beginOffset += timeZone.getDSTSavings();
		if (timeZone.inDaylightTime(endDate)) endOffset += timeZone.getDSTSavings();
		long endDays = (long)(endDate.getTime() + endOffset);
		long beginDays = (long)(beginDate.getTime() + beginOffset);
		return (endDays - beginDays);
	}

	/**
	 * 获得年份
	 * @param date
	 * @return int
	 * @author mxm
	 */
	public static final int getYear(Date date) {
		if (date == null) return 0;
		return getCalendarField(date, Calendar.YEAR);
	}

	/**
	 * 获得月份
	 * @param date
	 * @return int
	 * @author mxm
	 */
	public static final int getMonth(Date date) {
		if (date == null) return 0;
		return getCalendarField(date, Calendar.MONTH) + 1;
	}


	/**
	 * 获得天数
	 * @param date
	 * @return int
	 * @author mxm
	 */
	public static final int getDay(Date date) {
		if (date == null) return 0;
		return getCalendarField(date, Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获得小时
	 * @param date
	 * @return int
	 * @author mxm
	 */
	public static final int getHour(Date date) {
		if (date == null) return 0;
		return getCalendarField(date, Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获得分钟
	 * @param date
	 * @return int
	 * @author mxm
	 */
	public static final int getMinute(Date date) {
		if (date == null) return 0;
		return getCalendarField(date, Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获得秒
	 * @param date
	 * @return int
	 * @author mxm
	 */
	public static final int getSecond(Date date) {
		if (date == null) return 0;
		return getCalendarField(date, Calendar.SECOND);
	}

	/**
	 * 获得毫秒
	 * @param date
	 * @return int
	 * @author mxm
	 */
	public static final int getMilliSecond(Date date) {
		if (date == null) return 0;
		return getCalendarField(date, Calendar.MILLISECOND);
	}

	/**
	 * 返回给定日历字段的值
	 * @param date
	 * @param field
	 * @return int
	 * @author mxm
	 */
	public static final int getCalendarField(Date date, int field){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(field);
	}

	/**
	 * 添加或减去指定的时间量,按年
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date addYear(Date date, int value){
		return add(date, Calendar.YEAR, value);
	}

	/**
	 * 添加或减去指定的时间量,按月
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date addMonth(Date date, int value){
		return add(date, Calendar.MONTH, value);
	}

	/**
	 * 添加或减去指定的时间量,按日
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date addDay(Date date, int value){
		return add(date, Calendar.DAY_OF_MONTH, value);
	}

	/**
	 * 添加或减去指定的时间量,按小时
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date addHour(Date date, int value) {
		return add(date, Calendar.HOUR_OF_DAY, value);
	}

	/**
	 * 添加或减去指定的时间量,按分钟
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date addMinute(Date date, int value) {
		return add(date, Calendar.MINUTE, value);
	}

	/**
	 * 添加或减去指定的时间量,按秒
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date addSecond(Date date, int value) {
		return add(date, Calendar.SECOND, value);
	}

	/**
	 * 添加或减去指定的时间量,按毫秒
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date addMilliSecond(Date date, int value) {
		return add(date, Calendar.MILLISECOND, value);
	}

	/**
	 * 添加或减去指定的时间量
	 * @param date
	 * @param field
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date add(Date date, int field, int value){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, value);
		return cal.getTime();
	}

	/**
	 * 设置指定的时间量,按年
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date setYear(Date date, int value){
		return set(date, Calendar.YEAR, value);
	}

	/**
	 * 设置指定的时间量,按月
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date setMonth(Date date, int value){
		return set(date, Calendar.MONTH, value);
	}

	/**
	 * 设置指定的时间量,按日
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date setDay(Date date, int value){
		return set(date, Calendar.DAY_OF_MONTH, value);
	}

	/**
	 * 设置指定的时间量,按小时
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date setHour(Date date, int value) {
		return set(date, Calendar.HOUR_OF_DAY, value);
	}

	/**
	 * 设置指定的时间量,按分钟
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date setMinute(Date date, int value) {
		return set(date, Calendar.HOUR_OF_DAY, value);
	}

	/**
	 * 设置指定的时间量,按秒
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date setSecond(Date date, int value) {
		return set(date, Calendar.SECOND, value);
	}

	/**
	 * 设置指定的时间量,按毫秒
	 * @param date
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date setMilliSecond(Date date, int value) {
		return set(date, Calendar.MILLISECOND, value);
	}

	/**
	 * 设置指定的时间量
	 * @param date
	 * @param field
	 * @param value
	 * @return Date
	 * @author mxm
	 */
	public static final Date set(Date date, int field, int value){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(field, value);
		return cal.getTime();
	}

	/**
	 * 设置日期为当月的最后一天
	 * @param date
	 * @return Date
	 * @author mxm
	 */
	public static final Date lastDate(Date date){
		return addDay(addMonth(setDay(date, 1), 1), -1);
	}

	/**
	 * 设置日期为当月的第一天
	 * @param date
	 * @return Date
	 * @author mxm
	 */
	public static final Date firstDate(Date date){
		return setDay(date, 1);
	}

	/**
	 * 是否是当月最后一天
	 * @param date
	 * @return boolean
	 * @author mxm
	 */
	public static final boolean isLastDate(Date date) {
		Date _date = date;
		Date lastDate = lastDate(_date);
		if (compareByDate(date, lastDate) == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 是否是当月第一天
	 * @param date
	 * @return boolean
	 * @author mxm
	 */
	public static final boolean isFirstDate(Date date) {
		Date _date = date;
		Date firstDate = firstDate(_date);
		if (compareByDate(date, firstDate) == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 是否是同一天
	 * @param date1
	 * @param date2
	 * @return boolean
	 * @author mxm
	 */
	public static final boolean isSameDate(Date date1, Date date2) {
		if (compareByDate(getDateIgnoreTime(date1), getDateIgnoreTime(date2)) == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 是否是同一天月
	 * @param date1
	 * @param date2
	 * @return boolean
	 * @author mxm
	 */
	public static boolean isSameMonth(Date date1, Date date2) {
		if (getMonth(date1) == getMonth(date2)) {
			return true;
		}
		return false;
	}

	/**
	 * 是否是同一天年
	 * @param date1
	 * @param date2
	 * @return boolean
	 * @author mxm
	 */
	public static boolean isSameYear(Date date1, Date date2) {
		if (getYear(date1) == getYear(date2)) {
			return true;
		}
		return false;
	}

	/**
	 * 根据指定格式格式化时间
	 * @param date
	 * @param fmtString
	 * @return String
	 * @author mxm
	 */
	public static final String format(Date date, String fmtString) {
		DateFormat format = new SimpleDateFormat(fmtString);
		return format.format(date);
	}

	/**
	 * 格式格式化时间("yyyy-MM-dd")
	 * @param date
	 * @return String
	 * @author mxm
	 */
	public static final String formatDate(Date date) {
		if (null == date)
			return null;
		return format(date, "yyyy-MM-dd");
	}

	/**
	 * 格式格式化时间("yyyy-MM-dd HH:mm:ss")
	 * @param date
	 * @return String
	 * @author mxm
	 */
	public static final String formatTime(Date date) {
		if (null == date)
			return null;
		return format(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 根据指定格式解析时间
	 * @param dateString
	 * @param fmtString
	 * @return Date
	 * @author mxm
	 */
	public static final Date parse(String dateString, String fmtString){
		Date date = null;
		try {
			DateFormat format = new SimpleDateFormat(fmtString);
			date = format.parse(dateString);
		} catch (ParseException e) {
			logger.error("parse(String, String) parse error", e);
		}
		return date;
	}

	/**
	 * 解析时间("yyyy-MM-dd")
	 * @param dateString
	 * @return Date
	 * @author mxm
	 */
	public static final Date parseDate(String dateString){
		return parse(dateString, "yyyy-MM-dd");
	}



	public static Date addYears(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.addYears(date, amount);
	}

	public static Date addMonths(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.addMonths(date, amount);
	}

	public static Date addWeeks(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.addWeeks(date, amount);
	}

	public static Date addDays(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.addDays(date, amount);
	}

	public static Date addHours(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.addHours(date, amount);
	}

	public static Date addMinutes(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.addMinutes(date, amount);
	}

	public static Date addSeconds(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.addSeconds(date, amount);
	}

	public static Date addMilliseconds(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.addMilliseconds(date, amount);
	}

	public static Date truncate(Date date, int field) {
		return org.apache.commons.lang3.time.DateUtils.truncate(date, field);
	}

	public static int intervalYears(Date beginDate, Date endDate) {
		if (beginDate.after(endDate)) {
			Date tmp = endDate;
			endDate = beginDate;
			beginDate = tmp;
		}

		int endDateYear = getYear(endDate);
		int beginDateYear = getYear(beginDate);

		int interval = endDateYear - beginDateYear;

		return interval;
	}

	public static int intervalMonths(Date beginDate, Date endDate) {
		if (beginDate.after(endDate)) {
			Date tmp = endDate;
			endDate = beginDate;
			beginDate = tmp;
		}

		int endDateYear = getYear(endDate);
		int beginDateYear = getYear(beginDate);

		int intervalYear = endDateYear - beginDateYear;

		int endDateMonth = getMonth(endDate);
		int beginDateMonth = getMonth(beginDate);

		int intervalMonth = 0;
		if (endDateMonth < beginDateMonth) {
			--intervalYear;
			intervalMonth = endDateMonth + 12 - beginDateMonth;
		} else {
			intervalMonth = endDateMonth - beginDateMonth;
		}

		return (intervalYear * 12 + intervalMonth);
	}

	public static int intervalDays(Date beginDate, Date endDate) {
		if (beginDate.after(endDate)) {
			Date tmp = endDate;
			endDate = beginDate;
			beginDate = tmp;
		}

		beginDate = truncate(beginDate, 5);
		endDate = truncate(endDate, 5);

		long interval = endDate.getTime() - beginDate.getTime();

		return (int) (interval / 86400000L);
	}

	public static int intervalHours(Date beginDate, Date endDate) {
		if (beginDate.after(endDate)) {
			Date tmp = endDate;
			endDate = beginDate;
			beginDate = tmp;
		}

		beginDate = truncate(beginDate, 10);
		endDate = truncate(endDate, 10);

		long interval = endDate.getTime() - beginDate.getTime();

		return (int) (interval / 3600000L);
	}

	public static int intervalMinutes(Date beginDate, Date endDate) {
		if (beginDate.after(endDate)) {
			Date tmp = endDate;
			endDate = beginDate;
			beginDate = tmp;
		}
		return getIntervalMinutes(beginDate, endDate);
	}

	public static int getIntervalMinutes(Date beginDate, Date endDate) {
		beginDate = truncate(beginDate, 12);
		endDate = truncate(endDate, 12);

		long interval = endDate.getTime() - beginDate.getTime();

		return (int) (interval / 60000L);
	}

	public static long intervalSeconds(Date beginDate, Date endDate) {
		if (beginDate.after(endDate)) {
			Date tmp = endDate;
			endDate = beginDate;
			beginDate = tmp;
		}

		long interval = endDate.getTime() - beginDate.getTime();

		return (int) (interval / 1000L);
	}

	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar.get(1);
	}

	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return (calendar.get(2) + 1);
	}

	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar.get(5);
	}

	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar.get(10);
	}

	public static int getHourOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int day = calendar.get(11);
		return day;
	}

	public static Date setHourOfDay(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.setHours(date, amount);
	}

	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(12);
	}

	public static Date setMinute(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.setMinutes(date, amount);
	}

	public static int getSecond(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return calendar.get(13);
	}

	public static Date setSecond(Date date, int amount) {
		return org.apache.commons.lang3.time.DateUtils.setSeconds(date, amount);
	}

	public static String format(Date date, String pattern) {
		if (org.apache.commons.lang3.StringUtils.isEmpty(pattern))
			pattern = "yyyy-MM-dd HH:mm:ss";

		return DateFormatUtils.format(date, pattern);
	}

	public static String format(String dateString, String pattern, String desPattern)
			throws ParseException {
		if (org.apache.commons.lang3.StringUtils.isEmpty(pattern)) {
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		Date date = org.apache.commons.lang3.time.DateUtils.parseDate(dateString, new String[]{pattern});

		return format(date, desPattern);
	}

	public static Date convert(String date, String pattern)
			throws ParseException {
		if (org.apache.commons.lang3.StringUtils.isEmpty(pattern)) {
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		return sdf.parse(date);
	}

	public static Date parseDate(String dateString) {
		try {
			return org.apache.commons.lang3.time.DateUtils.parseDate(dateString, new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss", "yyyy-MM-dd HH:mm:ss.SSS"});
		} catch (ParseException e) {
			throw new InvalidParameterException(dateString + "不是一个正确格式的日期。");
		}
	}

	public static boolean isTimeBetween(Date startTime, Date endTime, Date dateTime) {
		return ((((dateTime.equals(startTime)) || (dateTime.after(startTime)))) && (dateTime.before(endTime)));
	}

	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
//		System.out.println(formatDate(parseDate("2010/3/6")));
//		System.out.println(getDate("yyyy年MM月dd日 E"));
//		long time = new Date().getTime()-parseDate("2012-11-19").getTime();
//		System.out.println(time/(24*60*60*1000));
	}
}
